from django.db import models
from django.contrib.auth.models import AbstractUser,UserManager as _UserManager

class UserManager(_UserManager):
    """
    rewrite  UserManager class
    """
    def create_superuser(self, username, password, email=None, **extra_fields):
        super().create_superuser(username=username, password=password, email=email, **extra_fields)
        # extra_fields.setdefault('is_staff', True)
        # extra_fields.setdefault('is_superuser', True)
        #
        # if extra_fields.get('is_staff') is not True:
        #     raise ValueError('Superuser must have is_staff=True.')
        # if extra_fields.get('is_superuser') is not True:
        #     raise ValueError('Superuser must have is_superuser=True.')
        #
        # return self._create_user(username, email, password, **extra_fields)

class Users(AbstractUser):
    """
    add mobile、email_active fields to Django users modules
    """
    mobile = models.CharField(max_length=11,unique=True,
                              help_text="手机号",verbose_name="手机号",
                              error_messages={
                                  'unique': "此手机号已被注册",},
                              )

    email_active = models.BooleanField(default=False,verbose_name="邮箱验证状态")

    class Meta:
        db_table = 'tb_users'
        verbose_name = "用户"
        verbose_name_plural = verbose_name

    objects = UserManager()

    REQUIRED_FIELDS = ['mobile']

    def __str__(self):
        return self.username


